package com.guawazi.widget;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.guawazi.R;


/**
 * created by luhui1hao 2017/08/29
 */
public class CommonTitleBar extends RelativeLayout {

    private TextView leftTv,titleTv;
    private LinearLayout leftSide;
    private RelativeLayout rootRl;
    private ImageView leftIv;
    private ImageView menuBtn;

    public CommonTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.layout_titlebar, this);
        leftSide = findViewById(R.id.ll_fctitlebar_left);
        leftIv = findViewById(R.id.iv_fctitlebar_left);
        leftTv = findViewById(R.id.left_tv);
        rootRl = findViewById(R.id.rl_titlebar_root);
        titleTv = findViewById(R.id.title_tv);
        menuBtn = findViewById(R.id.menu_btn);
    }


    /**
     * 设置左边文字
     */
    public void setLeftTv(String str){
        leftTv.setText(str);
    }

    /**
     * 设置左边 是否显示
     *
     * @param visibility
     */
    public void setLeftVisibility(int visibility) {
        leftSide.setVisibility(visibility);
    }


    /**
     * 隐藏左边的
     */
    public void hideLeftSide() {
        leftSide.setVisibility(View.GONE);
    }

    /**
     * 显示左边的
     */
    public void showLeftSide(){
        leftSide.setVisibility(VISIBLE);
    }

    /**
     * ®
     * 设置标题
     *
     * @param text
     * @return
     */
    public CommonTitleBar setTitle(String text) {
        titleTv.setText(text);
        return this;
    }

    /**
     * 设置titlebar的背景色
     *
     * @param color
     */
    public void setTitleBarBgColor(int color) {
        titleTv.setBackgroundColor(color);
    }



    /**
     * 设置左边返回点击finish的activity
     *
     * @param
     * @return
     */
    public CommonTitleBar setLeftClickFinish(OnClickListener listener) {
        leftSide.setOnClickListener(listener);
        return this;
    }

    /**
     * 设置左边返回点击finish的activity
     *
     * @param aty
     * @return
     */
    public CommonTitleBar setLeftClickFinish(final Activity aty) {
        leftSide.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                aty.finish();
                aty.overridePendingTransition(R.anim.in_left, R.anim.out_right);
            }
        });
        return this;
    }


    /**
     * 设置标题
     *
     * @param text
     * @return
     */
    public CommonTitleBar setTitle(String text, int color) {
        titleTv.setText(text);
        titleTv.setTextColor(getResources().getColor(color));
        return this;
    }

    /**
     * 导航栏设置是否显示
     *
     * @param isDisplay
     * @return
     */
    public CommonTitleBar setVisibility(boolean isDisplay) {
        if (isDisplay)
            rootRl.setVisibility(View.VISIBLE);
        else
            rootRl.setVisibility(View.GONE);
        return this;
    }

    /**
     * 设置右边菜单按键显示还是隐藏
     */
    public void setMenuVisibility(int visibility){
        menuBtn.setVisibility(visibility);
    }

    /**
     * 设置菜单按键点击事件
     */
    public void setMenuClick(OnClickListener listener){
        menuBtn.setOnClickListener(listener);
    }
}
